<?php

trait Trait_Api_Ship
{


    /**
     *  订单扩展信息
     * @param $order
     * @return array
     */
    public static function order_attach($order)
    {
        $data = array();
        //航线附加产品信息
        $attach_info = DB::select()->from('member_order_child')
            ->where('pid', '=', $order['id'])
            ->execute()
            ->current();

        $attach = unserialize($attach_info['attachinfo']);
        foreach ($attach as &$item) {
            $item['name'] = DB::select()->from('ship_line_additional')
                ->where('id', '=', $item['id'])
                ->execute()
                ->get('name');
        }
        $data['attach']   = $attach;
        $voyage           = DB::select()->from('ship_line_voyage')
            ->where('id', '=', $attach_info['voyageid'])
            ->execute()
            ->current();
        $suit             = DB::select()->from('ship_line_suit')
            ->where('id', '=', $attach_info['suitid'])
            ->execute()
            ->current();
        $ship_name        = DB::select()->from('ship')
            ->where('id', '=', $suit['shipid'])
            ->execute()
            ->get('title');
        $line             = DB::select('startcity', 'finaldestid')->from('ship_line')
            ->where('id', '=', $voyage['lineid'])
            ->execute()
            ->current();
        $startcity        = DB::select()->from('destinations')
            ->where('id', '=', $line['startcity'])
            ->execute()
            ->get('kindname');
        $destcity         = DB::select()->from('destinations')
            ->where('id', '=', $line['finaldestid'])
            ->execute()
            ->get('kindname');
        $room             = DB::select()->from('ship_room')
            ->where('id', '=', $suit['roomid'])
            ->execute()
            ->get('title');
        $lineInfo         = array(
            'date' => $order['usedate'],
            'ship' => $ship_name,
            'start_time' => $voyage['start_time'],
            'end_time' => $voyage['end_time'],
            'startcity' => $startcity,
            'destcity' => $destcity,
            'peoplenum' => $attach_info['peoplenum'],
            'dingnum' => $order['dingnum'],
            'room' => $room,
        );
        $data['lineInfo'] = $lineInfo;
        //人群
        $data['people'] = unserialize($attach_info['crowd']);
        //楼层
        $floor = unserialize($attach_info['floor']);
        foreach ($floor as &$val) {
            $val['name'] = DB::select('name')->from('ship_line_suit_floor_price')
                ->where('id', '=', $val['id'])
                ->execute()
                ->get('name');
        }
        $data['floors']   = $floor;
        $data['price']    = $order['pay_price'];
        $data['payprice'] = $order['pay_price'];

        return $data;
    }

    /**
     * 航次列表
     * @param $request_params
     * @return array
     * User: netman
     * Date: 2023/10/18 14:59
     */
    public function ship_line_list($request_params)
    {
        $params = array(
            'startcity' => $request_params['startcity'],//出发地id
            'dest_id' => $request_params['dest_id'],//目的地id
            'date' => $request_params['date'],//航次日期
            'lineid' => $request_params['line_id'],//航次日期
            'page' => $request_params['page'],//页码
            'pageSize' => $request_params['pagesize'],//每页数量
        );
        $model  = DB::select('ship_line.*', 'ship_line_voyage.day', 'ship_line_voyage.start_time',
            'ship_line_voyage.end_time', 'ship_line_voyage.day_num', 'ship_line_voyage.night')
            ->from('ship_line')
            ->join('ship_line_voyage', 'left')
            ->on('ship_line_voyage.lineid', '=', DB::expr('sline_ship_line.id'));

        $model->where('ship_line.ishidden', '=', 0);

        if ($params['dest_id']) {
            $model->where('finaldestid', '=', $params['dest_id']);
        }

        if ($params['startcity']) {
            $model->where(DB::expr(" AND find_in_set({$params['startcity']},sline_ship_line.startcity)"));
        }

        if ($params['date']) {
            $date = strtotime($params['date']);
            $model->where('ship_line_voyage.day', '=', $date);
        } else {
            $date = strtotime(date('Y-m-d'));
            $model->where('ship_line_voyage.day', '=', $date);
        }

        if ($params['lineid']) {
            $model->where('ship_line_voyage.lineid', '=', $params['lineid']);
        }

        $count              = $model->execute()->count();
        $params['page']     = $params['page'] < 1 ? 1 : $params['page'];
        $params['pageSize'] = empty($params['pageSize']) ? 10 : $params['pageSize'];
        $offset             = ($params['page'] - 1) * $params['pageSize'];
        $list               = $model->offset($offset)->limit($params['pageSize'])->execute()->as_array();
        if ($list) {
            $marketing_strategy = Model_Api_V2_Marketing_Strategy::get_instance(self::TYPE_ID, array('mid' => $request_params['mid']));

            foreach ($list as &$item) {
                $price = DB::select(DB::expr("min(price) as price"))->from('ship_line_suit_price')
                    ->where('lineid', '=', $item['id'])
                    ->where('day', '>=', $date)
                    ->execute()
                    ->get('price');

                $item['price']          = $price;
                $item['startcity_text'] = DB::select('kindname')->from('destinations')
                    ->where('id', '=', $item['startcity'])
                    ->execute()
                    ->get('kindname');
                $item['finaldest_text'] = DB::select('kindname')->from('destinations')
                    ->where('id', '=', $item['finaldestid'])
                    ->execute()
                    ->get('kindname');
                $item['date']           = date('Y-m-d', $item['day']);
                $item['day']            = date('m月d日', $item['day']);
                $item['ship_name']      = DB::select()->from('ship')
                    ->where('id', '=', $item['shipid'])
                    ->execute()
                    ->get('title');
                $item['ship_name']      = !empty($item['ship_name']) ? $item['ship_name'] : '';
                $item['trip_type']      = '单程';
                $item['trip_date']      = $item['day_num'] . '天' . $item['night'] . '晚';

                $item['marketing_strategy'] = $marketing_strategy->get_support_application($item['id']);
            }
        }
        $data = array(
            'total' => $count,
            'list' => $list,
            'page' => $params['page'],
            'pageSize' => $params['pageSize'],
        );
        return $data;
    }

    public function ship_line($params)
    {
        $shipid = $params['ship_id'];

        //查询构建
        $model = DB::select()->from('ship_line')
            ->where('shipid', '=', $shipid)
            ->where('ishidden', '=', 0);
        $list     = $model->execute()->as_array();
        $new_list = array();
        foreach ($list as $key => $item) {
            $check = DB::select()
                ->from('ship_line_suit_price')
                ->where('lineid', '=', $item['id'])
                ->where('day','>=',strtotime(date('Y-m-d')))
                ->limit(1)
                ->execute()
                ->current();
            if (empty($check)) {
                unset($list[$key]);
                continue;
            }
            $value = [];
            $day                    = DB::select('start_time', 'end_time')
                ->from('ship_line_voyage')
                ->where('day', '>=', strtotime(date('Y-m-d')))
                ->where('lineid', '=', $item['id'])
                ->execute()
                ->current();
            $value['start_time']     = !empty($day['start_time']) ? $day['start_time'] : '';
            $value['end_time']       = !empty($day['end_time']) ? $day['end_time'] : '';
            $value['startcity_text'] = DB::select('kindname')->from('destinations')
                ->where('id', '=', $item['startcity'])
                ->execute()
                ->get('kindname');
            $value['startcity_text'] = !empty($value['startcity_text']) ? $value['startcity_text'] : '';
            $value['finaldest_text'] = DB::select('kindname')->from('destinations')
                ->where('id', '=', $item['finaldestid'])
                ->execute()
                ->get('kindname');
            $value['finaldest_text'] = !empty($value['finaldest_text']) ? $value['finaldest_text'] : '';
            $value['id'] = $item['id'];
            $new_list[] = $value;
        }
        return array(
            'list' => $new_list
        );
    }


    /**
     * 根据航线和日期查询套餐
     * @param $params
     * @return array
     * User: netman
     * Date: 2023/10/18 15:07
     */
    public function date_suit($params)
    {
        $lineid = (int)$params['line_id'];
        $day    = $params['day'];

        //查询构建
        $list = DB::select('ship_line_suit.id','ship_line_suit.suitname','ship_line_suit.roomid','ship_line_suit.pay_way',
            'ship_line_suit.need_confirm', 'ship_line_suit_price.price', 'ship_line_suit_price.child_price','ship_line_suit.refund_restriction',
            'ship_line_suit_price.plus_price', 'ship_line_suit_price.number', 'ship_line_suit_price.plus_price',
            DB::expr('sline_ship_line_suit_price.id as suit_price_id'),
            'ship_line_suit_price.child_no_price', 'ship_line_suit_price.baby_price', 'ship_line_suit_price.parcel_price')->from('ship_line_suit')
            ->join('ship_line_suit_price', 'inner')
            ->on('ship_line_suit.id', '=', DB::expr('sline_ship_line_suit_price.suitid'))
            ->where('ship_line_suit.lineid', '=', $lineid)
            ->where('ship_line_suit_price.day', '=', strtotime($day))
            ->group_by('ship_line_suit.id')
            ->execute()
            ->as_array();
        if ($list) {
            foreach ($list as &$item) {
                $item['room'] = DB::select()->from('ship_room')
                    ->where('id', '=', $item['roomid'])
                    ->execute()
                    ->current();
                if ($item['room']) {
                    $item['room']['litpic'] = $this->img_url($item['room']['litpic']);
                    if ($item['room']['video_type'] == 1) {
                        $item['room']['video'] = !empty($item['room']['video']) ? $this->img_url($item['room']['video']) : '';
                    } else {
                        $item['room']['video'] = $item['room']['video_web'];
                    }

                    $piclist = explode(',', $item['room']['piclist']);
                    foreach ($piclist as &$val) {
                        $pic = explode('||', $val);
                        $val = $this->img_url($pic[0]);
                    }
                    $item['room']['piclist']   = $piclist;
                    $item['room']['kind_name'] = DB::select('title')->from('ship_room_kind')
                        ->where('id', '=', $item['room']['kindid'])
                        ->execute()
                        ->get('title');
                    $floors                    = DB::select('title')->from('ship_floor')
                        ->where('shipid', '=', $item['shipid'])
                        ->where('id', 'in', explode(',', $item['room']['floors']))
                        ->execute()
                        ->as_array();
                    $item['room']['floors']    = implode(',', array_column($floors, 'title'));
                    $item['room']['num']       = $item['number'];
                }
                $item['floors'] = '';
                $item['content'] = $this->content_add_domain($item['content']);
                $item['floor']  = DB::select()->from('ship_line_suit_floor_price')
                    ->where('suit_price_id', '=', $item['suit_price_id'])
                    ->execute()
                    ->as_array();
                if ($item['floor']) {
                    foreach ($item['floor'] as &$fo) {
                        $fo['num'] = 0;
                    }
                }
                //邮轮备注
//                $item['remark'] = DB::select()->from('ship')
//                    ->where('id', '=', $item['shipid'])
//                    ->execute()
//                    ->get('remark');
                //房型简介
                $item['desc'] = [
                    'area' => (!empty($item['room']['area'])?$item['room']['area']:''),
                    'peoplenum' => (!empty($item['room']['peoplenum'])?$item['room']['peoplenum']:''),
                    'bed' => (!empty($item['room']['bed'])?$item['room']['bed']:''),
                ];
                //房型标签
                $item['tag'] = [];
                switch ($item['pay_way']){
                    case 1:$item['tag'][] = '线上支付';break;
                    case 2:$item['tag'][] = '线下支付';break;
                    case 3:$item['tag'][] = '线上线下支付';break;
                }
                $item['tag'][] = $item['need_confirm'] == 0 ? '无需确认' : '需要商家确认库存';
                $item['tag'][] = $item['refund_restriction'] == 0 ? '可退' : '不可退';
                $item['litpic'] = $item['room']['litpic'];
            }
        }
        $data = array(
            'list' => $list
        );
        return $data;

    }

    /**
     * 获取航线的航期
     * @param $params
     * @return array
     * User: netman
     * Date: 2023/10/18 15:10
     */
    public function ship_line_voyage($params)
    {
        $lineid = $params['line_id'];
        //查询构建
        $start_time = strtotime(date('Y-m-d'));
        $end_time = strtotime('+180 day',$start_time);
        $model = DB::select('day',DB::expr('min(price) as price'),'number')->from('ship_line_suit_price')
            ->where('lineid', '=', $lineid)
            ->where('day', '>=', $start_time)
            ->where('day', '<', $end_time)
            ->where('price', '>', 0);
        $list  = $model->order_by('day', 'asc')->group_by('day')->execute()->as_array();
        if ($list) {
            $week_arr = [0=>'周日',1=>'周一',2=>'周二',3=>'周三',4=>'周四',5=>'周五',6=>'周六'];
            foreach ($list as &$item) {
                $item['date']  = date('Y-m-d', $item['day']);
                $item['week']  = $week_arr[date('w', $item['day'])];
                $item['md']  = date('m-d', $item['day']);
            }
        }
        return array(
            'list' => $list
        );
    }

    /**
     * 附加产品
     * @param $params
     * @return array[]
     * User: netman
     * Date: 2023/10/18 15:29
     */
    public function ship_line_additional($params)
    {
        $line_id = $params['line_id'];

        $add_ids = DB::select()->from('ship_line')
            ->where('id', '=', $line_id)
            ->execute()
            ->get('additional_ids');
        if (empty($add_ids)) {
            return array('list' => array());
        }
        $list = DB::select()->from('ship_line_additional')
            ->where('id', 'in', explode(',', $add_ids))
            ->execute()->as_array();

        $new_list = $res_list = array();
        if ($list) {
            foreach ($list as $val) {
                $val['num']                        = 0;
                $new_list[$val['type']]['child'][] = $val;
            }
            foreach ($new_list as $key => &$item) {
                $type              = DB::select('name', 'unit')
                    ->from('ship_line_additional_type')
                    ->where('id', '=', $key)
                    ->execute()
                    ->current();
                $item['type_name'] = $type['name'];
                foreach ($item['child'] as &$value) {
                    $value['unit'] = $type['unit'];
                }
            }
            foreach ($new_list as $v) {
                $res_list[] = $v;
            }
        }
        return array(
            'list' => $res_list
        );
    }

    /**
     * 航线切换列表
     * @param $params
     * @return array[]
     * User: netman
     * Date: 2023/10/18 15:35
     */
    public function all_ship_line($params)
    {
        $shipid = $params['shipid'] ?? 0;//邮轮ID
        $model  = DB::select('id', 'title', 'startcity', 'finaldestid')->from('ship_line');
        if ($shipid) {
            $model->where('shipid', '=', $shipid);
        }
        $list     = $model->execute()->as_array();
        $new_list = $res_list = array();
        if ($list) {
            foreach ($list as &$item) {
                $item['startcity_text'] = DB::select('kindname')->from('destinations')
                    ->where('id', '=', $item['startcity'])
                    ->execute()
                    ->get('kindname');
                $item['finaldest_text'] = DB::select('kindname')->from('destinations')
                    ->where('id', '=', $item['finaldestid'])
                    ->execute()
                    ->get('kindname');
                $res_list[]             = $item['startcity_text'] . '-' . $item['finaldest_text'] . '-' . $item['startcity'] . '-' . $item['finaldestid'];
            }
        }
        if ($res_list) {
            $res_list = array_unique($res_list);
            foreach ($res_list as $value) {
                $text       = explode('-', $value);
                $res        = array(
                    'startcity_text' => $text[0],
                    'finaldest_text' => $text[1],
                    'startcity' => $text[2],
                    'finaldestid' => $text[3],
                );
                $new_list[] = $res;
            }
        }
        return array(
            'list' => $new_list,
        );
    }

    /**
     * 首页邮轮公司列表
     * @return array
     * User: netman
     * Date: 2023/10/18 15:42
     */
    public function index_ship_list()
    {
        $list = DB::select()->from('supplier')
            ->where('kindid', '=', 6)
            ->where('verifystatus', '=', 3)
            ->limit(8)
            ->execute()
            ->as_array();
        if ($list) {
            foreach ($list as &$item) {
                $item['litpic'] = $this->img_url($item['litpic']);
            }
        }
        return array(
            'list' => $list,
        );
    }

    /**
     * 推荐邮轮
     * @return array
     * User: netman
     * Date: 2023/10/18 15:43
     */
    public function index_ship_common()
    {
        $list = DB::select()->from('ship_line')
            ->where('ishidden', '=', 0)
            ->where('is_common', '=', 1)
            ->execute()
            ->as_array();
        if ($list) {
            $date = strtotime(date('Y-m-d'));
            foreach ($list as &$item) {
                $price             = DB::select('price')
                    ->from('ship_line_suit_price')
                    ->where('lineid', '=', $item['id'])
                    ->where('day', '>=', $date)
                    ->execute()
                    ->as_array();
                $min_price         = min(array_column($price, 'price'));
                $item['price']     = empty($min_price) ? 0 : $min_price;
                $item['litpic']    = $this->img_url($item['litpic']);
                $item['tags']      = array();
                $item['ship_name'] = DB::select()->from('ship')
                    ->where('id', '=', $item['shipid'])
                    ->execute()
                    ->get('title');
            }
        }

        return array(
            'list' => $list,
        );

    }

    /**
     * 首页邮轮行程
     * @return array
     * User: netman
     * Date: 2023/10/18 15:45
     */
    public function index_ship_line_list()
    {
        $list = DB::select()->from('ship_line')
            ->where('ishidden', '=', 0)
            ->limit(6)
            ->execute()
            ->as_array();
        $date = strtotime(date('Y-m-d'));
        if ($list) {
            foreach ($list as &$item) {
                $item['litpic'] = $this->img_url($item['litpic']);
                $price          = DB::select('price')
                    ->from('ship_line_suit_price')
                    ->where('lineid', '=', $item['id'])
                    ->where('day', '>=', $date)
                    ->execute()
                    ->as_array();
                $min_price      = min(array_column($price, 'price'));
                $item['price']  = empty($min_price) ? 0 : $min_price;
                $attr_ids       = explode(',', $item['attrid']);
                $attr_text      = DB::select('attrname')->from('ship_line_attr')
                    ->where('isopen', '=', 1)
                    ->where('pid', '>', 0)
                    ->where('id', 'in', $attr_ids)
                    ->execute()
                    ->as_array();

                $item['attrs_text'] = array_column($attr_text, 'attrname');

                $item['tags'] = array();
            }
        }
        return array(
            'list' => $list,
        );

    }

    /**
     * 首页轮播广告
     * @return array
     * User: netman
     * Date: 2023/10/18 15:47
     */
    public function index_top_ads()
    {
        $list = DB::select()->from('xcx_ads')
            ->where(DB::expr("find_in_set(0,client_no)"))
            ->where('prefix', '=', 'c_ship_index')
            ->where('number', '=', 1)
            ->execute()
            ->as_array();

        if ($list) {
            foreach ($list as &$item) {
                $item['adsrc'] = $this->img_url($item['adsrc']);
            }
        }
        return array(
            'list' => $list,
        );
    }

    /**
     * 邮轮公司详情
     * @param $params
     * User: netman
     * Date: 2023/10/18 15:54
     */
    public function ship_info($params)
    {
        $params['ship_id'];//邮轮公司id
        //详情
        $info = DB::select('title','content','litpic','piclist','sailtime','weight',
        'seatnum','length','width','floornum','speed','start','renovation_time')->from('ship')
            ->where('id', '=', $params['ship_id'])
            ->execute()
            ->current();
        if ($info) {
            if ($info['piclist']) {
                $pic_list = [];
                $list = explode(',', $info['piclist']);
                foreach ($list as $item) {
                    $pic = explode('||',$item);
                    $res = $this->img_url($pic[0]);
                    $pic_list[] = $res;
                }
                $info['piclist'] = $pic_list;
            }
            $info['litpic']  = $this->img_url($info['litpic']);
            $info['content'] = $this->content_add_domain($info['content']);
        }

        return array(
            'info' => $info
        );
    }

    /**
     * 邮轮公司热门产品
     * @param $params
     * @return array
     * User: netman
     * Date: 2023/10/18 16:01
     */
    public function company_hot_product($params)
    {
        $supp_id  = $params['supp_id'];//邮轮公司ID
        $page     = $params['page'];//邮轮公司ID
        $pagesize = $params['pagesize'];//邮轮公司ID

        $page     = $page < 1 ? 1 : $page;
        $pagesize = empty($pagesize) ? 10 : $pagesize;

        $model  = DB::select('id','title','litpic','iconlist','sellpoint','attrid')->from('ship_line')
            ->where('supplierlist', '=', $supp_id)
            ->where('ishidden', '=', 0)
            ->where('is_common', '=', 1);
        $count  = $model->execute()->count();
        $offset = ($page - 1) * $pagesize;
        $list   = $model->offset($offset)->limit($pagesize)->execute()->as_array();
        if ($list) {
            foreach ($list as &$item) {
                $item['litpic'] = $this->img_url($item['litpic']);
                //icon数据
                $item['icon_list'] = $this->icons($item['iconlist']);
                $price             = DB::select(DB::expr('min(price) as price'))
                    ->from('ship_line_suit_price')
                    ->where('lineid', '=', $item['id'])
                    ->where('day', '>=', strtotime(date('Y-m-d')))
                    ->where('price', '>', 0)
                    ->execute()
                    ->get('price');
                $item['price']     = empty($price) ? 0 : $price;
                if(!empty($item['attrid'])){
                    $attrid = DB::select('attrname')->from('ship_line_attr')
                        ->where('id', 'in', explode(',',$item['attrid']))
                        ->execute()
                        ->as_array();
                    $attrid = array_column($attrid,'attrname');
                }else{
                    $attrid = array();
                }
                $item['attr_tag'] = $attrid;
            }
        }
        return array(
            'total' => $count,
            'list' => $list
        );
    }

    /**
     * 订单合同
     * @param $params
     * @return array
     * User: netman
     * Date: 2023/10/18 16:02
     */
    public function order_contract($params)
    {
        $order_id = $params['order_id'];//订单ID
        $url      = DB::select()->from('contract_supervise')
            ->where('order_id', '=', $order_id)
            ->execute()
            ->get('viewURL');

        return array(
            'url' => $url,
        );
    }

    /**
     * 生成子订单编号
     */
    private function get_sub_ordersn($kind)
    {
        /* 选择一个随机的方案 */
        return 'SUB' . $kind . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    }

    /**
     * 栏目首页搜索框下推荐航线
     * @return array
     */
    public function index_search_hot_line($params)
    {
        $params['page_size'] = !empty($params['page_size']) ? $params['page_size'] : 6;
        $list = DB::select()->from('ship_line')
            ->where('ishidden', '=', 0)
            ->where('is_common','=',1)
            ->limit($params['page_size'])
            ->execute()
            ->as_array();
        $new_list = [];
        if ($list) {
            foreach ($list as $item) {
                $startcity = DB::select('kindname')->from('destinations')
                    ->where('id','=', $item['startcity'])
                    ->execute()
                    ->get('kindname');
                $finaldest = DB::select('kindname')->from('destinations')
                    ->where('id','=', $item['finaldestid'])
                    ->execute()
                    ->get('kindname');
                $new_list[] = [
                    'lines_name' => (!empty($startcity) && !empty($finaldest) ? $startcity . ' - ' . $finaldest : ''),
                    'startcity' => $item['startcity'],
                    'dest_id' => $item['finaldestid']
                ];
            }
        }
        return array(
            'list' => $new_list,
        );
    }
    /**
     * 栏目首页搜索框下邮轮系列
     * @return array
     */
    public function index_search_company_list($params)
    {

        $suppliers = Model_Ship::get_supplier();
        $out = [];
        foreach ($suppliers as $supplier) {
            $out[] = [
                'id' => $supplier['value'],
                'suppliername' => $supplier['name']
            ];
        }
        return array(
            'list' => $out,
        );
    }

    /**
     * 栏目首页爆款推荐航线
     * @param $params
     * @return array[]
     */
    public function index_common_list($params)
    {
        $params['page_size'] = !empty($params['page_size']) ? $params['page_size'] : 6;
        $list = DB::select()->from('ship_line')
            ->where('is_common','=',1)
            ->where('ishidden','=',0)
            ->limit($params['page_size'])
            ->execute()
            ->as_array();
        $new_list = [];
        if($list){
            foreach ($list as $item){
                $startcity_text = DB::select('kindname')->from('destinations')
                    ->where('id','=', $item['startcity'])
                    ->execute()
                    ->get('kindname');
                $finaldest_text = DB::select('kindname')->from('destinations')
                    ->where('id','=', $item['finaldestid'])
                    ->execute()
                    ->get('kindname');
                $voyage = DB::select(DB::expr('min(day) as start_date,max(day) as end_date'))->from('ship_line_voyage')
                    ->where('day','>=',strtotime(date('Y-m-d')))
                    ->where('day','<=',strtotime(date('Y-12-31')))
                    ->execute()
                    ->current();
                $price = DB::select(DB::expr('min(price) as price'))->from('ship_line_suit_price')
                    ->where('day','>=',strtotime(date('Y-m-d')))
                    ->where('price','>',0)
                    ->where('lineid','=',$item['id'])
                    ->and_where_open()
                    ->where('number','=',-1)
                    ->or_where('number','>',0)
                    ->and_where_close()
                    ->execute()
                    ->get('price');
                $supplier_name = DB::select('suppliername')->from('supplier')
                    ->where('id','=', $item['supplierlist'])
                    ->execute()
                    ->get('suppliername');
                $new_list[] = [
                    'startcity_text' => $startcity_text,
                    'finaldest_text' => $finaldest_text,
                    'start_date'     => date('m-d',$voyage['start_date']),
                    'end_date'       => date('m-d',$voyage['end_date']),
                    'price'          => $price,
                    'litpic'         => $this->img_url($item['litpic']),
                    'supplier_name'  => $supplier_name,
                    'line_name'      => $item['title'],
                    'id'             => $item['id']
                ];
            }
        }
        return array(
            'list' => $new_list,
        );
    }

    /**
     * 栏目首页特价推荐航线
     * @param $params
     * @return array[]
     */
    public function index_special_list($params)
    {
        $params['page_size'] = !empty($params['page_size']) ? $params['page_size'] : 6;
        $list = DB::select()->from('ship_line')
            ->where('is_special','=',1)
            ->where('ishidden','=',0)
            ->limit($params['page_size'])
            ->execute()
            ->as_array();
        $new_list = [];
        if($list){
            foreach ($list as $item){
                $startcity_text = DB::select('kindname')->from('destinations')
                    ->where('id','=', $item['startcity'])
                    ->execute()
                    ->get('kindname');
                $finaldest_text = DB::select('kindname')->from('destinations')
                    ->where('id','=', $item['finaldestid'])
                    ->execute()
                    ->get('kindname');
                $voyage = DB::select(DB::expr('min(day) as start_date,max(day) as end_date'))->from('ship_line_voyage')
                    ->where('day','>=',strtotime(date('Y-m-d')))
                    ->where('day','<=',strtotime(date('Y-12-31')))
                    ->execute()
                    ->current();
                $price = DB::select(DB::expr('min(price) as price'))->from('ship_line_suit_price')
                    ->where('day','>=',strtotime(date('Y-m-d')))
                    ->where('price','>',0)
                    ->where('lineid','=',$item['id'])
                    ->and_where_open()
                    ->where('number','=',-1)
                    ->or_where('number','>',0)
                    ->and_where_close()
                    ->execute()
                    ->get('price');
                $supplier_name = DB::select('suppliername')->from('supplier')
                    ->where('id','=', $item['supplierlist'])
                    ->execute()
                    ->get('suppliername');
                $new_list[] = [
                    'startcity_text' => $startcity_text,
                    'finaldest_text' => $finaldest_text,
                    'start_date'     => date('m-d',$voyage['start_date']),
                    'end_date'       => date('m-d',$voyage['end_date']),
                    'price'          => $price,
                    'litpic'         => $this->img_url($item['litpic']),
                    'supplier_name'  => $supplier_name,
                    'line_name'      => $item['title'],
                    'id'             => $item['id']
                ];
            }
        }
        return array(
            'list' => $new_list,
        );
    }

    /**
     * 栏目首页邮轮品牌列表
     * @param $params
     * @return array
     */
    public function company_list($params)
    {
        $params['page_size'] = !empty($params['page_size']) ? $params['page_size'] : 6;
        $kindid = DB::select('id')->from('supplier_kind')
            ->where('kindname','=','邮轮')
            ->limit($params['page_size'])
            ->execute()
            ->get('id');
        $list = DB::select('id','suppliername','litpic','logo')->from('supplier')
            ->where('kindid','=',$kindid)
            ->execute()
            ->as_array();
        if($list){
            foreach ($list as &$item){
                $item['litpic'] = $this->img_url($item['litpic']);
                $item['logo']   = $this->img_url($item['logo']);
            }
        }
        return array(
            'list' => $list,
        );
    }

    /**
     * 栏目首页评论列表
     * @return array[]
     */
    public function index_comment_list($params)
    {
        $params['page_size'] = !empty($params['page_size']) ? $params['page_size'] : 6;
        $list = DB::select('id','memberid','content','level','addtime','vr_nickname','vr_headpic')->from('comment')
            ->where('typeid','=',104)
            ->limit($params['page_size'])
            ->execute()
            ->as_array();
        $new_list = [];
        if($list){
            foreach ($list as $item){
                if ($item['memberid']) {
                    $member = DB::select('nickname','mobile','litpic')->from('member')
                        ->where('mid','=',$item['memberid'])
                        ->execute()
                        ->current();
                    $nickname = !empty($member['nickname']) ? $member['nickname']:$member['mobile'];
                    $litpic = $this->img_url($member['litpic']);
                } else {
                    $nickname = $item['vr_nickname'] ?? '匿名';
                    $litpic = $this->img_url($item['vr_headpic']);
                }

                $new_list[] = [
                    'id'            => $item['id'],
                    'content'       => mb_substr(strip_tags($item['content']),0,40),
                    'level'         => $item['level'],
                    'addtime'       => date('Y-m-d', $item['addtime']),
                    'nickname'      => $nickname,
                    'litpic'        => $litpic
                ];
            }
        }
        return array(
            'list' => $new_list,
        );
    }

    /**
     * 邮轮公司条件列表
     * @return array
     */
    public function query_supplier_list()
    {

        $list = Model_Ship::get_supplier();
        $out = [];
        foreach ($list as $l) {
            $out[] = [
                'id' => $l['value'],
                'suppliername' => $l['name']
            ];
        }
        return array(
            'list' => $out,
        );
    }

    /**
     * 获取所有目的地列表
     * @return array
     */
    public function dest_list()
    {
        $list = DB::select('destinations.id','destinations.kindname')->from('destinations')
            ->join('ship_line_kindlist','inner')
            ->on('destinations.id','=',DB::expr('sline_ship_line_kindlist.kindid'))
            ->execute()
            ->as_array();
        return array(
            'list' => $list,
        );
    }

    /**
     * 航线列表
     * @param $params
     * @return array
     */
    public function line_list($params)
    {
        $params['page'] = !empty($params['page']) ? $params['page'] : 1;
        $params['page_size'] = !empty($params['page_size']) ? $params['page_size'] : 10;
        $offset = ($params['page'] - 1) * $params['page_size'];
        //数据查询构建
        $model = DB::select('ship_line.*',DB::expr('min(sline_ship_line_suit_price.price) as price'))->from('ship_line')
            ->join('ship_line_suit_price','left')
            ->on('ship_line.id','=',DB::expr('sline_ship_line_suit_price.lineid'))
            //->where('ship_line_suit_price.day','>=',time())
            //->where('ship_line_suit_price.price','>',0)
            ->where('ship_line.ishidden', '=', 0);
        if(!empty($params['startcity'])){
            $model->where('ship_line.startcity','=',$params['startcity']);
        }
        if(!empty($params['dest_id'])){
            $model->where('ship_line.finaldestid','=',$params['dest_id']);
        }
        if(!empty($params['key'])){
            $model->where('ship_line.title','like',"%{$params['key']}%");
        }
        if(!empty($params['supplier_id'])){
            $model->where('ship_line.supplierlist','=',$params['supplier_id']);
        }
        if(!empty($params['attr'])){
                $attr = explode('_', $params['attr']);
                $model->and_where_open();
                foreach ($attr as $k => $at) {
                    if (is_numeric($at)) {
                        if ($k == 0) {
                            $model->where(DB::expr("find_in_set({$at},sline_ship_line.attrid)"));
                        } else {
                            $model->where(DB::expr(" OR find_in_set({$at},sline_ship_line.attrid)"));
                        }
                    } else {
                        if ($at == 'recommend') {
                            $model->where('ship_line.is_common','=',1);
                        }
                        if ($at == 'hot') {
                            $model->where('ship_line.is_special','=',1);
                        }

                    }

                }
                $model->and_where_close();
            }

        $model->group_by('ship_line.id');
        $count = $model->execute()->count();
        $params['order'] = !empty($params['order']) ? $params['order'] : 1;
        //默认排序
        if($params['order'] == 1){
            $model->order_by('id','DESC');
        }
        //价格排序 由高到低
        if($params['order'] == 2){
            $model->order_by('price','DESC');
        }
        //价格排序 由低到高
        if($params['order'] == 3){
            $model->order_by('price','ASC');
        }
        //销量排序 由高到低
        if($params['order'] == 4){
            $model->order_by('ship_line.bookcount','DESC');
        }
        //销量排序 由低到高
        if($params['order'] == 5){
            $model->order_by('ship_line.bookcount','ASC');
        }
        $list   = $model->offset($offset)->limit($params['page_size'])->execute()->as_array();
        $result = [];
        if($list){
            $marketing_strategy = Model_Api_V2_Marketing_Strategy::get_instance(self::TYPE_ID, array('mid'=> $params['mid']));
            foreach ($list as $item){
                $value = [
                    'id'        => $item['id'],
                    'line_name' => $item['title'],
                    'price'     => $item['price'],
                    'sellpoint' => $item['sellpoint'],
                    'litpic' => $this->img_url($item['litpic']),
                ];
                //邮轮名称
                $value['ship_name'] = DB::select('title')->from('ship')
                    ->where('id', '=', $item['shipid'])
                    ->execute()
                    ->get('title');
                //icon数据
                $value['icon_list'] = $this->icons($item['iconlist']);
                //属性数据
                $attr                        = DB::select('attrname')->from('ship_line_attr')
                    ->where('id', 'in', explode(',', $item['attrid']))
                    ->execute()
                    ->as_array();
                $value['attr_list']          = array_column($attr, 'attrname');
                $value['marketing_strategy'] = $marketing_strategy->get_support_application($item['id']);
                $result[] = $value;
            }
        }
        return array(
            'list'         => $result,
            'count'        => $count,
            'current_page' => $params['page']
        );
    }

    /**
     * 获取起始目的地列表
     * @return array[]
     */
    public function start_dest_list()
    {
        $list = DB::select('startcity','finaldestid')->from('ship_line')
            ->where('ishidden','=',0)
            ->execute()
            ->as_array();
        $result = $sd = [];
        foreach ($list as $item){
            $startcity_text = DB::select('kindname')->from('destinations')
                ->where('id', '=', $item['startcity'])
                ->execute()
                ->get('kindname');
            $finaldest_text = DB::select('kindname')->from('destinations')
                ->where('id', '=', $item['finaldestid'])
                ->execute()
                ->get('kindname');
            $sd[] = $startcity_text.'-'.$finaldest_text.'-'.$item['startcity'].'-'.$item['finaldestid'];
        }
        $sd = array_unique($sd);
        foreach ($sd as $val){
            $v = explode('-',$val);
            $result[] = array(
                'start_name' => $v[0],
                'dest_name'  => $v[1],
                'start_id'   => $v[2],
                'dest_id'    => $v[3]
            );
        }
        return array(
            'list' => $result
        );
    }

    /**
     * 航期列表
     * @param $params
     * @return array
     */
    public function date_list($params)
    {
        $params['page'] = !empty($params['page']) ? $params['page'] : 1;
        $params['page_size'] = !empty($params['page_size']) ? $params['page_size'] : 10;
        $offset = ($params['page'] - 1) * $params['page_size'];
        $day = !empty($params['day']) ? strtotime($params['day']) : strtotime(date('Y-m-d'));
        //数据查询构建
        $model = DB::select('ship_line.id','ship_line.title','ship_line.startcity','ship_line.startcity',
            'ship_line.finaldestid','ship_line.shipid','ship_line_suit_price.price','ship_line_suit_price.voyageid')
            ->from('ship_line')
            ->group_by('ship_line.id')
            ->join('ship_line_suit_price','left')
            ->on('ship_line.id','=',DB::expr('sline_ship_line_suit_price.lineid'))
            ->where('ship_line_suit_price.day','=',$day)
            ->where('ship_line_suit_price.price','>',0)
            ->where('ship_line.ishidden', '=', 0);
        if(!empty($params['start_id'])){
            $model->where('ship_line.startcity','=',$params['start_id']);
        }
        if(!empty($params['dest_id'])){
            $model->where('ship_line.finaldestid','=',$params['dest_id']);
        }
        $count = $model->execute()->count();
        $list = $model->order_by('ship_line_suit_price.price','ASC')
            ->offset($offset)
            ->limit($params['page_size'])
            ->execute()->as_array();
        if($list){
            $marketing_strategy = Model_Api_V2_Marketing_Strategy::get_instance(self::TYPE_ID, array('mid'=> $params['mid']));
            foreach ($list as &$item){
                $item['start_text']         = DB::select('kindname')->from('destinations')
                    ->where('id', '=', $item['startcity'])
                    ->execute()
                    ->get('kindname');
                $item['dest_text']          = DB::select('kindname')->from('destinations')
                    ->where('id', '=', $item['finaldestid'])
                    ->execute()
                    ->get('kindname');
                $voyage                     = DB::select()->from('ship_line_voyage')
                    ->where('id', '=', $item['voyageid'])
                    ->execute()
                    ->current();
                $item['start_time']         = $voyage['start_time'];
                $item['end_time']           = $voyage['end_time'];
                $item['date']               = $voyage['day_num'] . '天' . $voyage['night'] . '晚';
                $item['ship_name']          = DB::select('title')->from('ship')
                    ->where('id', '=', $item['shipid'])
                    ->execute()
                    ->get('title');
                $item['marketing_strategy'] = $marketing_strategy->get_support_application($item['id']);
            }
        }
        return array(
            'list'         => $list,
            'count'        => $count,
            'current_page' => $params['page']
        );
    }

    /**
     * 邮轮公司详情
     * @param $params
     * @return array
     */
    public function company_info($params)
    {
        $info = DB::select('id','suppliername','content')->from('supplier')
            ->where('id','=',$params['supplier_id'])
            ->execute()
            ->current();
        $info['content'] = $this->content_add_domain($info['content']);
        $ship_list = DB::select('id','title','weight','seatnum','floornum','sailtime','litpic','content')->from('ship')
            ->where('supplierlist','=',$params['supplier_id'])
            ->where('ishidden','=',0)
            ->execute()
            ->as_array();
        if($ship_list){
            foreach ($ship_list as &$val){
                $val['litpic']  = $this->img_url($val['litpic']);
                $val['content'] = $this->content_add_domain($val['content']);
            }
        }
        $info['ship_list'] = $ship_list;
        return array(
            'info' => $info
        );
    }
}